﻿/// <reference path="config.js" />
'use strict';

/* Controllers */

angular.module('app')
  .controller('AppCtrl', ['$scope', '$translate', '$localStorage', '$window', 'globalService', 'tabService', function ($scope, $translate, $localStorage, $window, globalService, tabService) {
      // add 'ie' classes to html
      var isIE = !!navigator.userAgent.match(/MSIE/i);
      isIE && angular.element($window.document.body).addClass('ie');
      isSmartDevice( $window ) && angular.element($window.document.body).addClass('smart');
      // config
      $scope.app = {
          name: '术语字典管理',
          version: '1.3.3',
          // for chart colors
          color: {
              primary: '#7266ba',
              info:    '#23b7e5',
              success: '#27c24c',
              warning: '#fad733',
              danger:  '#f05050',
              light:   '#e8eff0',
              dark:    '#3a3f51',
              black:   '#1c2b36'
          },
          settings: {
              themeID: 1,
              navbarHeaderColor: 'bg-black',
              navbarCollapseColor: 'bg-white-only',
              asideColor: 'bg-black',
              headerFixed: true,
              asideFixed: false,
              asideFolded: false,
              asideDock: false,
              container: false
          }
      }

      // save settings to local storage
      if ( angular.isDefined($localStorage.settings) ) {
          $scope.app.settings = $localStorage.settings;
      } else {
          $localStorage.settings = $scope.app.settings;
      }
      if (angular.isDefined($localStorage.color)) {
          $scope.app.color = $localStorage.color;
      } else {
          $localStorage.color = $scope.app.color;
      }
      $scope.$watch('app.settings', function(){
          if( $scope.app.settings.asideDock  &&  $scope.app.settings.asideFixed ){
              // aside dock and fixed must set the header fixed.
              $scope.app.settings.headerFixed = true;
          }
          // save to local storage
          $localStorage.settings = $scope.app.settings;
      }, true);

      // angular translate
      $scope.lang = { isopen: false };
      $scope.langs = { en: 'English', de_DE: 'German', it_IT: 'Italian', zh_CN: '简体中文' };
      //$scope.langs = { zh_CN: '简体中文' };
      $scope.selectLang = $scope.langs[$translate.proposedLanguage()] || "简体中文";
      $scope.setLang = function(langKey, $event) {
          // set the current lang
          $scope.selectLang = $scope.langs[langKey];
          // You can change the language during runtime
          $translate.use(langKey);
          $scope.lang.isopen = !$scope.lang.isopen;
      };

      function isSmartDevice( $window )
      {
          // Adapted from http://www.detectmobilebrowsers.com
          var ua = $window['navigator']['userAgent'] || $window['navigator']['vendor'] || $window['opera'];
          // Checks for iOs, Android, Blackberry, Opera Mini, and Windows mobile devices
          return (/iPhone|iPod|iPad|Silk|Android|BlackBerry|Opera Mini|IEMobile/).test(ua);
      }

      angular.extend(angular, globalService);
  }])

app.controller('UserController', ['$scope', 'userService', function ($scope, userService) {
    $scope.User = userService.User;
}]);

app.directive("bodyResize", function ($interval, $log, $rootScope,tabService) {
    return (function () {
        return {
            restrict: 'A',
            link: function (scope, element, attrs) {
                if ($rootScope.treeTimeMethod == null) {
                    $rootScope.treeTimeMethod = $interval(function () {
                        var selectedTab = tabService.getTabByMenuID();
                        if (selectedTab != null) {
                            var tabControl = angular.element("#" + "myMenu" + selectedTab.id);
                            if (tabControl != null) {
                                var contentElement = tabControl.find(".contentElement");
                                if (contentElement != null) {
                                    if (contentElement.find(".abn-tree").length > 0) {
                                        setSplitterLeft(contentElement, selectedTab);
                                        setSplitterRight(contentElement, selectedTab);
                                    }
                                }
                            }
                        }
                    }, 1000);
                }
                $rootScope.$on('$destroy', function () {
                    $interval.cancel($rootScope.treeTimeMethod);
                });
            }
        }
    })();
})

function setSplitterLeft(contentElement, selectedTab) {
    var topmargin = 152;
    var footheight = 50;

    if (contentElement.hasClass("hasSplitter")) {
        var line = contentElement.find(".ui-drag-width");
        var leftpanel = contentElement.find(".ui-drag-width-left");
        var treepanel = contentElement.find(".tree-panel");

        leftpanel.css("height", ($(window).height() - topmargin - footheight) + "px");
        treepanel.css("height", leftpanel.height() - 60 + "px");
    }
}

function setSplitterRight(contentElement, selectedTab) {
    if (contentElement.hasClass("hasSplitter")) {
        var line = contentElement.find(".ui-drag-width");
        var rightpanel = contentElement.find(".ui-drag-width-right");
        rightpanel.css("width", ($(window).width() - line[0].offsetLeft) + "px");
    }
}

function setAutoTableHeight(contentElement) {
    var body = contentElement.find(".dataTables_scrollBody");
    if (body.length <= 0) {
        return;
    }

    var rightcontenthead = contentElement.find(".right-content-heading");
    var rightcontentheadheight = 0;
    if (rightcontenthead.length > 0) {
        rightcontentheadheight = rightcontenthead[0].offsetHeight + 10;
    }
    var gridheadpanel = contentElement.find(".panel-heading");
    var gridheadpanelheight = 0;
    if (gridheadpanel.length > 0) {
        gridheadpanelheight = gridheadpanel[0].offsetHeight;
    }
    for (var i = 0; i < body.length; i++) {
        if (body[i].clientHeight == 0) {
            continue;
        }
        var head = contentElement.find(".dataTables_scrollHead")[i];
        var wrapper=contentElement.find(".dataTables_wrapper")[i];
        var search = $(wrapper).find(".row")[0];
        var foot = $(wrapper).find(".row")[2];

        $(body[i]).css("height", 100 + "px");
    }
}

function setAutoTableHead(contentElement) {
    var body = contentElement.find(".dataTables_scrollBody");
    if (body.length <= 0) {
        return;
    }

    for (var i = 0; i < body.length; i++) {
        if (body[i].clientHeight == 0) {
            continue;
        }
        var head = contentElement.find(".dataTables_scrollHeadInner")[i];
        var item = $(body[i]).find("table");
        $(head).css("width", $(item[i]).width() + "px");
    }
}

function setAutoTableHeadClass(contentElement) {
    var body = contentElement.find(".dataTables_scrollBody");
    if (body.length <= 0) {
        return;
    }
    body.find("thead tr th").removeClass("sorting sorting_asc sorting_disabled sorting_desc");
}

app.controller('UserController', ['$scope', 'userService', function ($scope, userService) {
    $scope.User = userService.User;
}]);

app.directive('setFocus', function () {
    //return function (scope, element) {
    //    element[0].focus();
    //};
    return {
        scope: false,
        link: function (scope, element) {
            scope.$watch('isFocus', function (newValue, oldValue, scope) {
                if (newValue) {
                    element[0].focus();
                }
            }, true);
        }
    }
});
